//
//  XYResourceController.swift
//  XYReadBook
//
//  Created by tsaievan on 2017/9/1.
//  Copyright © 2017年 XWJ. All rights reserved.
//

import UIKit
import YYImage

/// 资源控制器

/// TODO 自定义cell
class XYResourceController: XYViewController {
    
    let Identifier = "ResourceIdentifier"
 
    private lazy var tableView:UITableView = {
        let tableView = UITableView.init(frame: CGRect.zero, style: .grouped)
        tableView.backgroundColor = UIColor.hm_color(withRed: 242, green: 242, blue: 242)
        tableView.dataSource = self
        tableView.delegate = self
        tableView.separatorStyle = .none
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: self.Identifier)
        return tableView
    }()
    
    
    /// lazy 搜索栏 设置代理
    private lazy var searchView:UISearchBar = {
        var  searchView = UISearchBar()
        searchView.frame = CGRect.init(x: 8, y: 26, width: ScreenWidth - 16 - 44, height: 30)
        searchView.placeholder = "书名、作者名、关键字"
        
        // MARK 清除搜索栏里的阴影
        for subView in searchView.subviews {
            for view in subView.subviews {
                if view.isKind(of:NSClassFromString("UISearchBarBackground")!) {
                    let imageView = view as! UIImageView
                    imageView.removeFromSuperview()
                }
            }
        }
        return searchView
    }()
    
    /// 数组如果不指定类型,也会自动推导
    let dataArr = [["icon":"resoure_classification","text":"分类"],
                   ["icon":"resoure_list","text":"排行榜"],
                   ["icon":"resoure_project","text":"专题"],
                   ["icon":"resoure_signin","text":"签到"]]
    
    var isStatusBarLightContent:Bool = true {
        didSet{
            
            if isStatusBarLightContent != oldValue {
                
                setStatusBarStyle()
            }
        }
    }
    
    
    /// 设置状态栏颜色
    private func setStatusBarStyle() {
        
        if isStatusBarLightContent {
            
            UIApplication.shared.setStatusBarStyle(UIStatusBarStyle.lightContent, animated: true)
            
        }else{
            
            UIApplication.shared.setStatusBarStyle(UIStatusBarStyle.default, animated: true)
        }
    }
    
    
    deinit {
        XYLog(messsage: "我被销毁了")
    }
    
   
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
        // Do any additional setup after loading the view.
    }
    
    
    func setupUI() {
        
        navigationItem.titleView = searchView
        view.addSubview(tableView)
        
    }

    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        tableView.frame = view.bounds
    }

    func pushClassifyVC() {
        let vc = XYClassifyController()
        vc.navigationItem.title = "资源"
        vc.hidesBottomBarWhenPushed = true
        let item = UIBarButtonItem(title: "返回", style: .plain, target: self, action: nil)
        navigationItem.backBarButtonItem = item
        navigationController?.pushViewController(vc, animated: true)
    }
}



extension XYResourceController:UITableViewDataSource,UITableViewDelegate{
    func numberOfSections(in tableView: UITableView) -> Int {
        return dataArr.count
    }
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
              return 1
    }
    
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = UITableViewCell.init(style: .value1, reuseIdentifier: Identifier)

        cell.textLabel?.text = dataArr[indexPath.section]["text"]
            
        cell.imageView?.image = UIImage.init(named: dataArr[indexPath.section]["icon"]!)
        cell.accessoryType = .disclosureIndicator
        return cell
    }
    
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 44
    }
    
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 10
    }
    
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 10
    }
  
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        switch indexPath.section {
        case 0:
            pushClassifyVC()
            break
        case 1: break
        case 2: break
        case 3: break

        default: break
            
        }
    }
}

